﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Security;
using System.Text;

namespace SharePointUtilitiesForDevelopers.Service
{
    /// <summary>
    /// A factory class for creating a <see cref="Binding"/> configured for a SharePoint web service.
    /// </summary>
    internal static class SharePointServiceBindingFactory
    {
        /// <summary>
        /// Creates a <see cref="Binding"/> configured for a SharePoint web service.
        /// </summary>
        /// <returns>A <see cref="Binding"/>.</returns>
        internal static Binding CreateInstance(EndpointAddress address)
        {
            var binding = new BasicHttpBinding();
            binding.CloseTimeout = TimeSpan.FromMinutes(1);
            binding.OpenTimeout = TimeSpan.FromMinutes(1);
            binding.SendTimeout = TimeSpan.FromMinutes(1);
            binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
            binding.AllowCookies = false;
            binding.MaxBufferSize = 2147483647;
            binding.MaxBufferPoolSize = 524288;
            binding.MaxReceivedMessageSize = 2147483647;
            binding.MessageEncoding = WSMessageEncoding.Text;
            binding.TextEncoding = Encoding.UTF8;
            binding.TransferMode = TransferMode.Buffered;
            binding.ReaderQuotas.MaxBytesPerRead = 2147483647;
            binding.ReaderQuotas.MaxDepth = 2147483647;
            binding.ReaderQuotas.MaxStringContentLength = 2147483647;
            binding.ReaderQuotas.MaxArrayLength = 2147483647;
            binding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
            binding.Security.Mode = address.Uri.Scheme == "https"
                                        ? BasicHttpSecurityMode.Transport
                                        : BasicHttpSecurityMode.TransportCredentialOnly;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
            binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Ntlm;
            binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
            binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
            binding.UseDefaultWebProxy = true;

            return binding;
        }
    }
}

                //<binding name="ViewsSoap" closeTimeout="00:01:00" openTimeout="00:01:00"
                //    receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                //    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                //    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                //    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                //    useDefaultWebProxy="true">
                //    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                //        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                //    <security mode="None">
                //        <transport clientCredentialType="None" proxyCredentialType="None"
                //            realm="" />
                //        <message clientCredentialType="UserName" algorithmSuite="Default" />
                //    </security>
                //</binding>